package com.cd.cms.common.utils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;

/**
 * 客户端上传图片存在临时文件夹
 * @author Administrator
 *
 */
public class UploadImagesClientUtils {
	public static List<String> uploadTemporary(MultipartFile[] myimages,HttpServletRequest request){
		List<String> list = new ArrayList<String>();
		for(MultipartFile myimage : myimages){
			String filename ="";
            if(myimage.isEmpty()){  
            	list.add("0");
            }else{  
                String realPath = request.getSession().getServletContext().getRealPath("/WEB-INF/upload");  
                filename = uuidName(myimage.getOriginalFilename());
                try {
					FileUtils.copyInputStreamToFile(myimage.getInputStream(), new File(realPath, filename));
					list.add(realPath+"\\"+filename);
				} catch (IOException e) {
					list.add("0");
					e.printStackTrace();
				}  
            }  
        }  
		return list;
	}
	public static String uuidName(String filename) {
		String img = filename.substring(filename.lastIndexOf(".")+1);
		UUID uuid = UUID.randomUUID();
		return uuid.toString() + "." +img;
	}
}
